package com.cn.zsl.redis;

import java.util.List;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

/**
 * Hello world!
 *
 */
public class App {
	
	public static void main(String[] args) {

		Jedis client = new Jedis("192.168.220.128");
		client.set("aa", "44");
		System.out.println(client.get("aa"));
		client.close();

		
		
		client.watch("bb");
		Transaction multi = client.multi();
		multi.set("aa", "1");
		multi.incr("aa");
		multi.set("bb", "ceshi");
		List<Object> exec = multi.exec();
		// exec返回的是null说明键的值被其它线程修改了.
		if (exec == null) {
			System.out.println("值被修改了,事务没有执行。。。。");
			// testIncr();
		} else {
			System.out.println("正常执行....");
		}
		

	}
	
	
	
	

	/**
	 * 逻辑判断： 同一个IP地址在60秒内最多登陆10次
	 */
	public boolean login(String ip) {

		//
		Jedis client = new Jedis("192.168.220.128", 6379);

		String numStr = client.get(ip);

		if (numStr == null) {

			client.set(ip, "1");
			// 设置KEY过期时间为60秒
			client.expire(ip, 60);

		} else {

			int num = Integer.parseInt(numStr);
			if (num > 10) {
				System.out.println("访问受限！");
				return false;
			} else {
				client.incr(ip);
			}

		}

		return true;

	}

}
